using System;
using System.Collections;
using System.Text;
using System.Collections.Generic;

namespace Util
{
	public static class StringUtil
	{
		public delegate object GetterDelegate<T>(T val);

		public static string Join<T>(string separator, IEnumerable vals, GetterDelegate<T> getter)
		{
			StringBuilder res = new StringBuilder();
			foreach (T val in vals)
			{
				if (res.Length != 0 && separator != null)
					res.Append(separator);
				res.Append(getter(val).ToString());
			}
			return res.ToString();
		}

		public static string Join<T>(IEnumerable vals, GetterDelegate<T> getter)
		{
			return Join(null, vals, getter);
		}

		public static string Join(IEnumerable vals)
		{
			return Join(null, vals);
		}

		public static string Join(string separator, IEnumerable vals)
		{
			return Join<object>(separator, vals, delegate(object val) { return val; });
		}

		public static string Concatenate(string separator, IEnumerable vals)
		{
			StringBuilder res = new StringBuilder();
			foreach (object val in vals)
			{
				if (separator != null)
					res.Append(separator);
				res.Append(val.ToString());
			}
			return res.ToString();
		}

		public static bool Exists(string val, Predicate<char> predicate)
		{
			foreach (char ch in val)
			{
				if (predicate(ch))
					return true;
			}
			return false;
		}
	}
}
